package com.devgroupvn.guinness.youtube;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.devgroupvn.guinness.Application;

public class ActivitySearchYoutube extends LinearLayout
{

	Button btnShare;
	EditText edSearch;
	List<YoutubeData> listYoutube = new ArrayList<YoutubeData>();
	ListYoutubeAdapter adapter;
	Context mContext;
	ListView mListView;
	Activity mActivity;

	public ActivitySearchYoutube(Context context, Activity _activity)
	{
		super(context);
		mContext = context;
		mListView = new ListView(context);
		mActivity = _activity;
		init();
//		getListYoutube(0, 20);
		new AsynGetYoutube(0).execute();
	}

	private void init()
	{
		LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		addView(mListView, params);
		setAdapter();
//		mListView.setAdapter(adapter);
		GetonClick();
		

	}

	private void getView()
	{
	}

	private void setAdapter()
	{
		adapter = new ListYoutubeAdapter(mActivity, listYoutube);
		mListView.setAdapter(adapter);
	}

	private void GetonClick()
	{

		mListView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			{
				Application.getInstance().putData("listYoutube",listYoutube);
				Intent message = new Intent(mContext, VideoStreamingPlayer.class);
				message.putExtra(VideoStreamingPlayer.TYPE, VideoStreamingPlayer.TYPE_PLAYLIST_SERVICE);
				message.putExtra(VideoStreamingPlayer.FIRST_LINK, listYoutube.get(arg2).getVideoLink());
				message.putExtra(VideoStreamingPlayer.INDEX_FIRST_LINK, arg2);
				mActivity.startActivity(message);
			}

		});
	}
	private class AsynGetYoutube extends AsyncTask<Void, Void, Void>
	{
		int page;
		AsynGetYoutube (int _page)
		{
			page = _page;
		}
		@Override
		protected Void doInBackground(Void... params)
		{
			getListYoutube(page);
			return null;
		}
		@Override
		protected void onPostExecute(Void result)
		{
			adapter.notifyDataSetChanged();
//			setAdapter();
			super.onPostExecute(result);
		}
		
	}

	private void getListYoutube(int page)
	{
//		String maxresult = "50";
		String url = "http://gdata.youtube.com/feeds/api/users/GuinnessWorldRecords/uploads";
		String object = "";
		Map<String, String> params = new HashMap<String, String>();
		params.put("start-index", "1");
//		params.put("max-results", maxresult);
		params.put("v", "2");
		params.put("format", "5");
		params.put("alt", "jsonc"); // parse json
		String data = getParamsString(params);
		url = url + "?" + data;
		try
		{
			object = getResponseText(url);
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (object != null)
		{
			try
			{
				listYoutube.addAll(Util.getYoutubeItem(object));
			}
			catch (JSONException e)
			{
				e.printStackTrace();
			}
		}
	}

	private String getResponseText(String stringUrl) throws IOException
	{
		StringBuilder response = new StringBuilder();

		URL url = new URL(stringUrl);
		HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
		if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK)
		{
			BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()));// ,8192
			String strLine = null;
			while ((strLine = input.readLine()) != null)
			{
				response.append(strLine);
			}
			input.close();
		}
		return response.toString();
	}

	private String getParamsString(Map<String, String> params)
	{
		if (params == null)
			return null;
		String ret = "";
		for (String key : params.keySet())
		{
			String value = params.get(key);
			ret += key + "=" + URLEncoder.encode(value) + "&";
		}
		return ret;
	}

}
